//
//  OvenViewController.m
//  Fragrance
//
//  Created by mark on 15/6/1.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "OvenViewController.h"
#import <UIImageView+AFNetworking.h>
@interface OvenViewController ()

@end

@implementation OvenViewController

- (void)dealloc
{
    [_dataArray release];
    [_dataArray1 release];
    [_dataArray2 release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"正餐";
    self.view.backgroundColor = [UIColor clearColor];
    
    [TAOverlay showOverlayWithLabel:nil Options:TAOverlayOptionOverlayTypeActivityLeaf];
    
    
    [self downLoadThisPage];
}


- (void)downLoadThisPage
{


    NSString *str1 = @"http://ibaby.ipadown.com/api/food/food.plan.detail.php?random=1";
    str1 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str1];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:80];
    request.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        [TAOverlay hideOverlay];
        
        self.dataArray = [NSMutableArray array];
        self.dataArray = [OvenJieXi getOvenModel:data];
        
        self.dataArray1 = [NSMutableArray array];
        self.dataArray1 = [OvenTwoJieXi getOvenTwoModel:data];
        
        self.dataArray2 = [NSMutableArray array];
        self.dataArray2 = [OvenThreeJieXi getOvenThreeModel:data];
        
        [self initOvenPageView];
        
    }];
}

- (void)initOvenPageView
{
    
    
    OvenTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50)];
    
    [self.view addSubview:OvenTableView];
    OvenTableView.delegate = self;
    OvenTableView.dataSource = self;
    [OvenTableView release];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row == 0) {
        static NSString *indentifier = @"cell";
        OvenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
        
        if (cell == nil) {
            cell = [[OvenTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier] ;
        }
        
        
        UIImage *image = [UIImage imageNamed:@"beijing3"];
        UIImageView *imageview1 = [[UIImageView alloc] initWithImage:image];
        cell.backgroundView = imageview1;
        OvenModel *model = [self.dataArray objectAtIndex:indexPath.row];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = model.title;
        cell.yingyangLabel.text = model.yingyang;
        [cell.thumbLabel setImageWithURL:[NSURL URLWithString:model.thumb] placeholderImage:[UIImage imageNamed:@"jiazaitu"]];
        cell.thumbLabel.contentMode = UIControlContentHorizontalAlignmentFill;
        [imageview1 release];
        
        return cell;
    }else if (indexPath.row  >= 1 && indexPath.row <= 4){
        static NSString *indentifier = @"cell1";
        OvenTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
        
        if (cell == nil) {
            cell = [[OvenTwoTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier] ;
        }
        
        UIImage *image = [UIImage imageNamed:@"beijing3"];
        UIImageView *imageview1 = [[UIImageView alloc] initWithImage:image];
        cell.backgroundView = imageview1;
        [imageview1 release];
        OvenTwoModel *model2 = [self.dataArray1 objectAtIndex:(indexPath.row - 1)];
        

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = model2.title;
        cell.yingyangLabel.text = model2.yingyang;
        [cell.thumbLabel setImageWithURL:[NSURL URLWithString:model2.thumb] placeholderImage:[UIImage imageNamed:@"jiazaitu"]];
        cell.thumbLabel.contentMode = UIControlContentHorizontalAlignmentFill;
        return cell;
    }else
    {
        static NSString *indentifier = @"cell2";
        OvenThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
        
        if (cell == nil) {
            cell = [[OvenThreeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier];
        }
        
        UIImage *image = [UIImage imageNamed:@"beijing3"];
        UIImageView *imageview1 = [[UIImageView alloc] initWithImage:image];
        cell.backgroundView = imageview1;
        [imageview1 release];
        OvenThreeModel *model3 = [self.dataArray2 objectAtIndex:(indexPath.row - 5)];
        cell.titleLabel.text = model3.title;
        cell.yingyangLabel.text = model3.yingyang;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.thumbLabel setImageWithURL:[NSURL URLWithString:model3.thumb] placeholderImage:[UIImage imageNamed:@"jiazaitu"]];
        cell.thumbLabel.contentMode = UIControlContentHorizontalAlignmentFill;
        
        return cell;
    }
    
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1 + self.dataArray1.count + self.dataArray2.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        OvenDetailViewController *OvenD = [[OvenDetailViewController alloc] init];
        
        OvenModel *model = [self.dataArray objectAtIndex:0];
        OvenD.model = model;
        [self.navigationController pushViewController:OvenD animated:YES];
        [OvenD release];
    }else if (indexPath.row  >= 1 && indexPath.row <= 4){
        OvenDetailViewController *OvenD = [[OvenDetailViewController alloc] init];
        OvenModel *model1 = [self.dataArray1 objectAtIndex:indexPath.row - 1];
        OvenD.model = model1;
        [self.navigationController pushViewController:OvenD animated:YES];
        [OvenD release];
    }else{
        OvenDetailViewController *OvenD = [[OvenDetailViewController alloc] init];
        OvenModel *model2 = [self.dataArray2 objectAtIndex:indexPath.row - 5];
        OvenD.model = model2;
        [self.navigationController pushViewController:OvenD animated:YES];
        [OvenD release];

    }
    
    
    
    

    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
